document.body.addEventListener('touchmove', function (e) {
    if(e._isScroller){
        return;
    }
    e.preventDefault(); 
}, {
    passive: false
});

function attemptJump(url, type) {
    if (type === 'scheme') {
        // 创建隐藏的iframe进行跳转
        const iframe = document.createElement('iframe');
        iframe.style.display = 'none';
        iframe.src = url;
        document.body.appendChild(iframe);

        // 检查是否跳转成功
        setTimeout(function() {
            document.body.removeChild(iframe);
            showStatus('跳转完成', 'success');

            // 如果还在当前页面，提示用户
            setTimeout(function() {
                if (!document.hidden) {
                    showStatus('如果未成功跳转，请点击确定手动打开', 'warning');
                    if (confirm('是否手动打开小程序？')) {
                        window.location.href = url;
                    }
                }
            }, 1000);

        }, 500);
    }
}

$(window).on("load", function(){
    //var isMobile = navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/i);
    var userAgent = navigator.userAgent;
    var isMobile = /Mobi/.test(userAgent);
    if(isMobile) {
       $(".isMobile").css("display","none");
    } else {
        $(".isMobile").css("display","flex");
    }

    $(window).resize(function(){
        var userAgent = navigator.userAgent;
        var isMobile = /Mobi/.test(userAgent);
        if(isMobile) {
            $(".isMobile").css("display","none");
        } else {
            $(".isMobile").css("display","flex");
        }
    })

    var isPlay = true;
    var myAudio=$("#myAudio");
    var busAudio=$("#busAudio");  
    //myAudio[0].play(); 

    $('.btn_music').click(function(){  

        //isPlay = !isPlay;
        //if(!isPlay){
        if (myAudio[0].paused) {  
            myAudio[0].play();
            $(".btn_music").addClass('on'); 
        }else{
            myAudio[0].pause();
            $(".btn_music").removeClass('on'); 
        }
        
     
    }) 
    $(document).on('touchstart', function(event) {
        // 阻止默认行为（如果需要）
        event.preventDefault();
        
        if (isPlay && myAudio[0].paused) {  
            myAudio[0].play();
            $(".btn_music").addClass('on'); 
            isPlay = false;
        }
        // 获取触摸点信息
        //var touch = event.originalEvent.touches[0];
        //console.log('Touch started at X: ' + touch.pageX + ', Y: ' + touch.pageY);
    });


    //全局变量
    var choujiang=false;//记录是否抽过奖
    // 小程序配置
    const miniProgramConfig = {
        path: '/modules-bus/pages/callBus',
        query: 'from=h5&source=button',
        envVersion: 'develop' // release:正式, trial:体验, develop:开发
    };
    var indexSwiper = new Swiper('#swiper-container1', {
        direction:'vertical',
        slidesPerView:"auto", //个数
        effect : 'fade',
        fadeEffect: {
            crossFade: true,
        },  
        initialSlide: 0,
        //allowTouchMove: false,
       // touchMoveStopPropagation : false,
        //touchRatio:0.5,
        //longSwipesRatio: 0.4,
        //threshold:50,
        //followFinger:false,
        //observer:true,
        //observeParents:true, 
        allowSlidePrev: false,
        allowSlideNext: false, 
        //mousewheel: true, 
 
        on:{
            init: function(swiper){
                var index=this.activeIndex; 
                this.slides.eq(index).find(".section").addClass('active'); 
                //swiperAnimateCache(this);
                //swiperAnimate(this);
                $(".section1 .title").addClass("animated zoomIn");
                $(".section1 .paizi").addClass("animated slideInLeft");
                $(".section1 .bus").addClass("animated slideInRight");
                $(".section1 .btn_start").addClass("animated1 fadeInUp");
              
                $(".btn_start").click(function(){
                    busAudio[0].play();
                     
                    setTimeout(function(){
                        indexSwiper.allowSlideNext= true;  
                        indexSwiper.slideNext();
                        busAudio[0].pause();
                    },1000) 
                }) 
                
            },
            slideChangeTransitionStart: function () {
                var index=this.activeIndex;
                this.slides.find(".section").removeClass('active');
                this.slides.eq(index).find(".section").addClass("active"); 
                 //swiperAnimate(this);
                //console.log(index)  
                if(index==1){
                    $(".section2 .page2_title").addClass("animated fadeInUp");
                    $(".section2 .bashi").addClass("animated slideInLeft");
                    $(".section2 .shuzhi").addClass("animated slideInLeft");
                    $(".section2 .flower1").addClass("animated slideInLeft");
                    setTimeout(function(){
                        $(".section2 .flower1").removeClass("animated slideInLeft");
                        $(".section2 .flower1").addClass("animated1 zoomOut"); 
                        $(".section2 .flower2").addClass("animated1 zoomInSmall");
                    },1000)
                    
                }
                if(index==2){
                    indexSwiper.allowSlideNext= false;  
                    $(".slide1_lupai1").addClass("animated slideInLeft");
                    $(".slide1_lupai2").addClass("animated fadeInRightBig");
                    $(".slide1_bus").addClass("animated fadeInRightBig");
                    $(".slide1_wenan1").addClass("animated fadeInUp"); 
                    $(".slide1_wenan2").addClass("animated fadeInUp"); 
                    $(".slide1_wenan3").addClass("animated fadeInleft"); 
                    $(".slide1_wenan4").addClass("animated fadeInRight"); 
                    $(".slide1_img1").addClass("animated fadeInRightBig"); 
                    $(".slide1_img2").addClass("animated zoomIn"); 
                    $(".slide1_img3").addClass("animated slideInUp"); 
                    $(".slide1_img4").addClass("animated slideInUp"); 
                    $(".slide1_img5").addClass("animated slideInRight");
                }  
                if(index==3){
                    indexSwiper.allowSlideNext= false;  
                    $(".section4 .title").addClass("animated fadeInUp");
                    $(".section4 .bus").addClass("animated slideZoomInLeft");
                    $(".section4 .toy").addClass("animated2 zoomIn");
                    $(".section4 .btn_hongbao").addClass("animated2 fadeInUpBig");

                }
                if(index==4){  
                    indexSwiper.allowSlidePrev= true;   
                    $(".section5 .wenan").addClass("animated fadeInUp"); 
                    //$(".section5 .bus").addClass("animated slideInRight");
                    $(".section5 .hongbao_box").addClass("animated2 zoomIn");
                    $(".section5 .page_btn1").addClass("animated1 fadeInUp");   
                    $(".section5 .page_btn2").addClass("animated1 fadeInUp");  
                    $(".page_btn1").click(function(){ 
                        indexSwiper.slideTo(0, 1000, false);
                        location.reload(true);
                    })
                    $(".page_btn2").click(function(){
                        // 获取小程序配置
                        $.ajax({
                            url: '/web/miniprogram/url-link',
                            type: 'POST',
                            contentType: 'application/json',
                            data: JSON.stringify({
                                path: miniProgramConfig.path,
                                query: miniProgramConfig.query,
                                isExpire: false
                            }),
                            success: function(response) {
                                if(response.code == 200){
                                    const urlLink = response.data.urlLink;
                                    // 直接跳转
                                    window.location.href = urlLink;
                                }
                            },
                            error: function(xhr, status, error) {
                                alert('网络错误: ' + error, 'error');
                            }
                        });
                    })
                }else{
                    indexSwiper.allowSlidePrev= false;
                }
                if(this.activeIndex==3 && choujiang==true){
                    indexSwiper.allowSlideNext= true;
                    this.slideTo(4, 1000, false);
                    $("#result").html("您已抽过奖<br>常银陪伴不缺席"); 
                }

            }, 
            reachEnd: function(){
                var index=this.activeIndex;
                this.slides.eq(index).removeClass('active');
                this.slides.removeClass("active").eq(index+1).addClass('active');  
                
            }, 
            fromEdge: function(){
                 
            }
        }
    });
    $(".sectioin2").on('touchstart', function(event) {
  
        indexSwiper.slideNext();
    });
    //第三屏长图
    setTimeout(function(){
        var mySwiper2 = new Swiper('#swiper-container2',{ 
            direction: 'vertical',
            nested:true,

            //resistanceRatio: 0,
            slidesPerView: 'auto',
            freeMode: true, 
            parallax : true,
            // edgeSwipeDetection: true,
            // touchReleaseOnEdges:true,
            // watchSlidesProgress: true,
            resistance: true,
            resistanceRatio: 0.85,
            force3D: true,
            speed: 300,
            followFinger: true,
            shortSwipes: false,
            longSwipesRatio: 0.5,
            scrollbar: {
              el: '.swiper-scrollbar',
            },
            on:{
                init: function(){  
                },
                touchMove: function(swiper,event){
                    var move=-this.getTranslate()
                    //console.log(this.getTranslate()); 
                    if(move<=725){
                        // $(".parallax-bus1").css("transform","translate3d("+ move*-0.3 +"px,"+ move*0.1 +"px,0)")
                        $('.parallax-bus1').css("display","block");
                        $('.parallax-bus2').css("display","none");
                        $('.parallax-bus3').css("display","none");
                        $('.parallax-bus4').css("display","none");
                    }
                    if(move>725 && move<=1460){
                        $('.parallax-bus1').css("display","none");
                        $('.parallax-bus2').css("display","block");
                        $('.parallax-bus3').css("display","none");
                        $('.parallax-bus4').css("display","none");
                    }else if(move>1460 && move<=1850) {
                        $('.parallax-bus1').css("display","none");
                        $('.parallax-bus2').css("display","none");
                        $('.parallax-bus3').css("display","block");
                        $('.parallax-bus4').css("display","none");
                    }else if(move>1850) {
                        $('.parallax-bus1').css("display","none");
                        $('.parallax-bus2').css("display","none");
                        $('.parallax-bus3').css("display","none");
                        $('.parallax-bus4').css("display","block");
                    }
                },
                slideChange: function () {
                    var index=this.activeIndex;  
                    
                    if(index==0){
                        $(".slide1_lupai1").addClass("animated slideInLeft");
                        $(".slide1_lupai2").addClass("animated fadeInRightBig");
                        $(".slide1_bus").addClass("animated fadeInRightBig"); 
                        $(".slide1_wenan1").addClass("animated fadeInUp"); 
                        $(".slide1_wenan2").addClass("animated fadeInUp"); 
                        $(".slide1_wenan3").addClass("animated fadeInleft"); 
                        $(".slide1_wenan4").addClass("animated fadeInRight"); 
                        $(".slide1_img1").addClass("animated fadeInRightBig"); 
                        $(".slide1_img2").addClass("animated1 fadeInUp"); 
                        $(".slide1_img3").addClass("animated2 fadeInUp"); 
                        $(".slide1_img4").addClass("animated slideInUp"); 
                        $(".slide1_img5").addClass("animated slideInRight"); 
                    }
                    if(index==1){ 

                        $(".slide2_lupai1").addClass("animated slideInLeft");
                        $(".slide2_lupai2").addClass("animated fadeInRightBig");
                        $(".slide2_bus").addClass("animated slideZoomInRight");
                        $(".slide2_wenan1").addClass("animated slideInLeft"); 
                        $(".slide2_wenan2").addClass("animated slideInLeft"); 
                        $(".slide2_wenan3").addClass("animated fadeInRight");  
                        $(".slide2_img1").addClass("animated zoomIn"); 
                        $(".slide2_img2").addClass("animated1 zoomIn"); 
                        $(".slide2_img3").addClass("animated2 zoomIn"); 
                        $(".slide2_img4").addClass("animated2 slideInUp");   
                    }
                    if(index==2){
                       
                        $(".slide3_lupai1").addClass("animated1 slideInLeft");
                        $(".slide3_lupai2").addClass("animated2 fadeInRightBig");
                        $(".slide3_bus").addClass("animated slideZoomInLeft");
                        $(".slide3_wenan1").addClass("animated slideInLeft"); 
                        $(".slide3_wenan2").addClass("animated slideInLeft"); 
                        $(".slide3_wenan3").addClass("animated fadeInRight");
                        $(".slide3_wenan4").addClass("animated fadeInRight");    
                        $(".slide3_img1").addClass("animated slideInLeft"); 
                        $(".slide3_img2").addClass("animated1 slideInUp"); 
                        $(".slide3_img3").addClass("animated2 zoomIn"); 
                        $(".slide3_img4").addClass("animated zoomIn"); 
                        $(".slide3_img5").addClass("animated zoomIn"); 
                        $(".slide3_img6").addClass("animated1 zoomIn"); 
                        $(".slide3_img7").addClass("animated2 zoomIn"); 
                        $(".slide3_img8").addClass("animated2 slideInLeft");  
                    }
                    if(index==3){
                        $(".slide4_lupai1").addClass("animated1 slideInLeft");
                        $(".slide4_lupai2").addClass("animated2 fadeInRightBig");
                        $(".slide4_bus").addClass("animated slideZoomInRight");
                        $(".slide4_wenan1").addClass("animated slideInLeft"); 
                        $(".slide4_wenan2").addClass("animated slideInLeft"); 
                        $(".slide4_wenan3").addClass("animated fadeInRight");
                        $(".slide4_wenan4").addClass("animated slideInUp");    
                        $(".slide4_img1").addClass("animated slideInRight"); 
                        $(".slide4_img2").addClass("animated1 slideInRight"); 
                        $(".slide4_img3").addClass("animated2 zoomIn"); 
                        $(".slide4_img4").addClass("animated zoomIn"); 
                        $(".slide4_img5").addClass("animated2 zoomIn"); 
                        $(".slide4_img6").addClass("animated2 zoomIn"); 
                        $(".slide4_img7").addClass("animated2 zoomIn"); 
                        $(".slide4_img8").addClass("animated2 zoomIn");
                    }
                    if(index==4){
                        $(".slide5_lupai1").addClass("animated slideInLeft");
                        $(".slide5_wenan1").addClass("animated slideInLeft"); 
                        $(".slide5_wenan2").addClass("animated slideInRight");     
                        $(".slide5_img1").addClass("animated slideInRight"); 
                        $(".slide5_img2").addClass("animated1 slideInUp"); 
                        $(".slide5_img3").addClass("animated2 slideInLeft");  

                    }
                    
                } ,
                reachEnd: function(){
                    var index=this.activeIndex;
                    indexSwiper.allowSlideNext= true; 
                    $(".slide5_lupai1").addClass("animated slideInLeft"); 
                    $(".slide5_wenan1").addClass("animated slideInLeft"); 
                    $(".slide5_wenan2").addClass("animated slideInRight");     
                    $(".slide5_img1").addClass("animated slideInRight"); 
                    $(".slide5_img2").addClass("animated1 slideInUp"); 
                    $(".slide5_img3").addClass("animated2 slideInLeft");
                }, 

            }
          })
        },500);  
    
    //第四页点击左上角活动规则按钮
    $(".btn_rules").click(function(){
        $(".rulesPop").addClass("active");
        $('.btn_music').css("z-index","0");
    })
    //第四页点击活动规则关闭按钮
    $(".rules_close").click(function(){
        $(".rulesPop").removeClass("active");
        $('.btn_music').css("z-index","80");
    })
    //红包抽奖

    var prizes = [
        { name: "0.88", probability: 0.1 },
        { name: "0.78", probability: 0.1 },
        { name: "0.68", probability: 0.1 },
        { name: "0.58", probability: 0.1 },
        { name: "0.28", probability: 0.1 },
        { name: "0.18", probability: 0.1 },
        { name: "0", probability: 0.4 }//0表示未中奖
    ];
    //随机选择奖项，为每个奖项计算一个概率范围,累加每个奖项的概率
    var totalProbability = 0;
    prizes.forEach(function(prize) {
        totalProbability += prize.probability;
        prize.maxProbability = totalProbability; // 设置每个奖项的最大概率值
    });

    //})
    function drawPrize() {
        var randomValue = Math.random(); // 生成一个0到1之间的随机数
        var selectedPrize;
        prizes.some(function(prize) {
            if (randomValue <= prize.maxProbability) {
                selectedPrize = prize;
                return true; // 找到匹配的奖项后停止迭代
            }
            return false; // 继续检查下一个奖项
        });
        return selectedPrize;
    }
    //function showResult() {
    $(".btn_hongbao").on('click',function(){
        var step = $("#step").val();
        var openid = $("#openid").val();
        if(step == 1){
            //未登录 跳转微信登录
            location.href = "/web/auth-url?state="+openid
        }else if(step == 2){
            //已登录 抽奖
            var token = $("#token").val();
            $.ajax({
                url: "/web/lottery?uuid=" + token,
                type: "GET",
                dataType: "json",
                success: function(res) {
                    console.log(res)
                    if(res.code == 200){
                        lotteryInfo = res.data.lotteryInfo;
                        const status = lotteryInfo.status
                        const money = lotteryInfo.money
                        indexSwiper.allowSlideNext= true;
                        indexSwiper.slideNext(); //点击抽奖后页面翻到下一页显示结果

                        if(status == 'N'){//prize.name等于0表示未中奖
                            $("#result").html("这次未中没关系<br>常银陪伴不缺席");
                        }else{
                            $("#result").html("恭喜您抽中<span class='zhongjiang'>" + money + "</span>元<br>这份幸运，为‘相守’而来");
                            let trans = res.data.trans;
                            const signature = trans.signature;
                            const jsAppId = trans.jsAppId;
                            const nonceStr = trans.nonceStr;
                            const timestamp = trans.timestamp;

                            const mchId = trans.mchId;
                            const appId = trans.appId;
                            const package = trans.packageInfo;

                            wx.config({
                                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                                appId: jsAppId, // 必填，服务号的唯一标识
                                timestamp: timestamp, // 必填，生成签名的时间戳
                                nonceStr: nonceStr, // 必填，生成签名的随机串
                                signature: signature,// 必填，签名
                                jsApiList: ['requestMerchantTransfer'] // 必填，需要使用的JS接口列表
                            });
                            wx.ready(function () {
                                wx.checkJsApi({
                                    jsApiList: ['requestMerchantTransfer'],
                                    success: function (res) {
                                        if (res.checkResult['requestMerchantTransfer']) {
                                            WeixinJSBridge.invoke('requestMerchantTransfer', {
                                                    mchId: mchId,
                                                    appId: appId,
                                                    package: package,
                                                },
                                                function (res) {
                                                    if (res.err_msg === 'requestMerchantTransfer:ok') {
                                                        // res.err_msg将在页面展示成功后返回应用时返回success，并不代表付款成功
                                                    }
                                                }
                                            );
                                        } else {
                                            alert('你的微信版本过低，请更新至最新版本。');
                                        }
                                    }
                                });
                            });
                        }
                    } else {
                        indexSwiper.allowSlideNext= true;
                        indexSwiper.slideNext(); //点击抽奖后页面翻到下一页显示结果
                        $("#result").html(res.msg);
                        $btn.prop('disabled', false).css('opacity', '1');
                    }
                },
                error: function(xhr, status, error) {
                    hideLoading();
                    console.log("抽奖请求错误:", error);
                    $btn.prop('disabled', false).css('opacity', '1');
                }
            });
        }
        // choujiang = true;//点击过抽奖按钮，表示已经抽过奖了
        // var prize = drawPrize(); // 调用抽奖函数
        // indexSwiper.allowSlideNext= true;
        // indexSwiper.slideNext(); //点击抽奖后页面翻到下一页显示结果
        //
        // if(prize.name==0){//prize.name等于0表示未中奖
        //     $("#result").html("这次未中没关系<br>常银陪伴不缺席");
        // }else{
        //     $("#result").html("恭喜您抽中<span>" + prize.name + "</span>元<br>这份幸运，为‘相守’而来");
        // }
    })
    //} 
})